package com.googlecode.guicehibernate.event.def;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

abstract class Times {

	static Logger logger = LoggerFactory.getLogger(TimeSettingSaveEventListener.class);
	
	static void setTime(Object object, Class<? extends Annotation> annotationClass, Date time) {
		Class<?> searchType = object.getClass();
		while (!Object.class.equals(searchType) && searchType != null) {
			Field[] fields = searchType.getDeclaredFields();
			for (Field field : fields) {
				boolean accessible = field.isAccessible();
				if (!accessible) {
					field.setAccessible(true);
				}
				if (field.isAnnotationPresent(annotationClass)) {
					try {
						field.set(object, time);
					} catch (Exception e) {
						logger.error("Failed to set " + annotationClass.getName() + " on " + object + ", ignored it", e);
					}
				}
				field.setAccessible(accessible);
			}
			searchType = searchType.getSuperclass();
		}
	}

}
